import 'package:flutter/material.dart';

class MultiTouchGestureDetector extends StatefulWidget {
  final Widget child;
  final Function(int fingers)? onPanDown;
  final Function(int fingers, double verticalDelta)? onPanUpdate;
  final Function(int fingers)? onScaleStart;
  final Function(int fingers, double scale)? onScaleUpdate;
  final Function(int fingers)? onScaleEnd;

  const MultiTouchGestureDetector({
    super.key,
    required this.child,
    this.onPanDown,
    this.onPanUpdate,
    this.onScaleStart,
    this.onScaleUpdate,
    this.onScaleEnd,
  });

  @override
  _MultiTouchGestureDetectorState createState() =>
      _MultiTouchGestureDetectorState();
}

class _MultiTouchGestureDetectorState extends State<MultiTouchGestureDetector> {
  int _activePointers = 0;
  bool _panDownTriggered = false;
  bool _panUpdateTriggered = false;
  bool _scaleStartTriggered = false;
  bool _scaleUpdateTriggered = false;
  bool _scaleEndTriggered = false;
  double _previousVerticalDelta = 0.0;
  bool _isScaling = false; // 标记当前是否在缩放模式

  void _handlePanDown(int fingers) {
    if ((fingers == 2 || fingers == 3) && !_isScaling && !_panDownTriggered) {
      widget.onPanDown?.call(fingers);
      _panDownTriggered = true;
    }
  }

  void _handlePanUpdate(int fingers, double verticalDelta) {
    if ((fingers == 2 || fingers == 3) && !_isScaling && !_panUpdateTriggered) {
      widget.onPanUpdate?.call(fingers, verticalDelta);
      _panUpdateTriggered = true;
    }
  }

  void _handleScaleStart(int fingers) {
    if ((fingers == 4 || fingers == 5) && !_scaleStartTriggered) {
      widget.onScaleStart?.call(fingers);
      _scaleStartTriggered = true;
      _isScaling = true; // 进入缩放模式
    }
  }

  void _handleScaleUpdate(int fingers, double scale) {
    if ((fingers == 4 || fingers == 5) && !_scaleUpdateTriggered) {
      widget.onScaleUpdate?.call(fingers, scale);
      _scaleUpdateTriggered = true;
    }
  }

  void _handleScaleEnd(int fingers) {
    if ((fingers == 4 || fingers == 5) && !_scaleEndTriggered) {
      widget.onScaleEnd?.call(fingers);
      _scaleEndTriggered = true;
      _isScaling = false; // 退出缩放模式
    }
  }

  @override
  Widget build(BuildContext context) {
    return Listener(
      onPointerDown: (PointerDownEvent event) {
        _activePointers++;
        _handlePanDown(_activePointers);
      },
      onPointerMove: (PointerMoveEvent event) {
        if ((_activePointers == 2 || _activePointers == 3) && !_isScaling && !_panUpdateTriggered) {
          double verticalDelta = event.delta.dy;
          if (verticalDelta > 0) {
            _handlePanUpdate(_activePointers, verticalDelta - _previousVerticalDelta);
            _panUpdateTriggered = true;
          }
          _previousVerticalDelta = verticalDelta;
        }
      },
      onPointerUp: (PointerUpEvent event) {
        _activePointers--;
        if (_activePointers < 2 || _activePointers < 3) {
          _panDownTriggered = false;
          _panUpdateTriggered = false;
        }
        if (_activePointers < 4 || _activePointers < 5) {
          _scaleStartTriggered = false;
          _scaleUpdateTriggered = false;
          _scaleEndTriggered = false;
        }
        _previousVerticalDelta = 0.0;
      },
      child: GestureDetector(
        onScaleStart: (details) {
          _handleScaleStart(_activePointers);
        },
        onScaleUpdate: (details) {
          _handleScaleUpdate(_activePointers, details.scale);
        },
        onScaleEnd: (details) {
          _handleScaleEnd(_activePointers);
        },
        child: widget.child,
      ),
    );
  }
}